package org.jwcloud.project.module.zx.matClient.service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.matClient.service.repository.MatClientRepository;
import org.jwcloud.project.module.zx.matClient.service.repository.MatSystemRepository;
import org.jwcloud.project.module.zx.workTime.service.repository.MatWorkTimeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jw.mid.mm.comm.common.controller.BaseController;

@Service
@Transactional
public class MatSystemServiceImpl extends BaseController implements MatSystemService{
	
	@Autowired
    public RptUtService rptUtService;
	@Autowired
	public MatSystemRepository matSystemRepository;
	@Override
	public Resp queryCommonList(Map param) {
		// TODO Auto-generated method stub
		return Resp.success(this.rptUtService.pageBuild(
				() -> this.matSystemRepository.queryCommonList(param), param));
	}
	@Override
	public Resp<Object> queryItemList_byBaseId(Map param) {
		// TODO Auto-generated method stub
		Map res = new HashMap();
		res.put("list", this.matSystemRepository.queryItemList_byBaseId(param));
		res.put("desc", this.matSystemRepository.queryItemList_byBaseId_total(param));
		return Resp.success(res);
	}
	@Override
	public Resp<Object> go_state(Map param) {
		// TODO Auto-generated method stub
		
		String now_state = (String) param.get("now_state");
		if("13".equals(now_state)) {//使用申请审批---加财务审批子流程
			param.put("type_flag", "sq");
			this.matSystemRepository.startSp(param);
		}
		if("23".equals(now_state)) {//预付款申请审批---加财务审批子流程
			param.put("type_flag", "yfk");
			this.matSystemRepository.startSp(param);
		}
		if("33".equals(now_state)) {//结算申请审批---加财务审批子流程
			param.put("type_flag", "js");
			this.matSystemRepository.startSp(param);
		}
		
		String go_state = String.valueOf(param.get("go_state"));
		String next_state = "";
		if("99".equals(go_state)) {
			if("14".equals(now_state)) {
				next_state = "199";
			}
			if("24".equals(now_state)) {
				next_state = "299";
			}
			if("34".equals(now_state)) {
				next_state = "399";
			}
		}
		if("2".equals(go_state)) {// || "99".equals(go_state)) {
			next_state = "";
		}else if("14".equals(now_state)//归档
				 || "24".equals(now_state)//归档
				 || "34".equals(now_state)//归档
				 ) {
			param.put("guidang_flag", "yes");//待删
		}
		else  {
			next_state = Integer.valueOf(now_state) + 1 + "";
		}
		param.put("next_state", next_state);
		
		this.matSystemRepository.go_state_updateProcessTip(param);
		
		this.matSystemRepository.go_state_done(param);//当前流程修改完成状态
		this.matSystemRepository.go_state_go(param);//下一节点修改初始状态
		this.matSystemRepository.go_state_update(param);//base表改状态
		//每步审批都有日志表
		this.matSystemRepository.go_state_insertProcessLog(param);
		this.matSystemRepository.go_state_insertProcessTip(param);//专门做时效处理
		
		
		//return Resp.success(this.matSystemRepository.go_state(param));
		return Resp.success(1);
	}
	@Override
	public Resp<Object> canYfk(Map param) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Resp<Object> canJs(Map param) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Resp<Object> queryOne(Map param) {
		// TODO Auto-generated method stub
		String sqType = (String) param.get("sqType");
		String flag = "";
		if("yfk".endsWith(sqType)) {
			flag = this.matSystemRepository.checkYfk(param);
			if(StringUtils.isNotEmpty(flag)) {
				return Resp.error(flag);
			}
		}else if("js".endsWith(sqType)) {
			flag = this.matSystemRepository.checkJs(param);
			if(StringUtils.isNotEmpty(flag)) {
				return Resp.error(flag);
			}
		}
		return Resp.success(this.matSystemRepository.queryOne_base(param));
	}
	@Override
	public Resp<Object> doYfk(Map param) {
		// TODO Auto-generated method stub
		this.matSystemRepository.doYfk(param);
		
		Map tipMap = new HashMap();//监控表
		tipMap.put("id", param.get("id"));
		tipMap.put("next_state", "20");
		this.matSystemRepository.go_state_insertProcessTip(tipMap);
		
		this.matSystemRepository.doYfk_updateProcess(param);
		return Resp.success(1);
	}
	@Override
	public Resp<Object> doJs(Map param) {
		// TODO Auto-generated method stub
		this.matSystemRepository.doJs(param);
		
		Map tipMap = new HashMap();//监控表
		tipMap.put("id", param.get("id"));
		tipMap.put("next_state", "20");
		this.matSystemRepository.go_state_insertProcessTip(tipMap);
		
		this.matSystemRepository.doJs_updateProcess(param);
		return Resp.success(1);
	}
	@Override
	public Resp<Object> queryMoneySpList(Map param) {
		// TODO Auto-generated method stub
		return Resp.success(this.rptUtService.pageBuild(
				() -> this.matSystemRepository.queryMoneySpList(param), param));
	}
	@Override
	public Resp<Object> moneySpGo(Map param) {
		// TODO Auto-generated method stub
		return Resp.success(this.matSystemRepository.moneySpGo(param));
	}
	@Override
	public Resp<Object> queryBankList(Map param) {
		// TODO Auto-generated method stub
		return Resp.success(this.rptUtService.pageBuild(
				() -> this.matSystemRepository.queryBankList(param), param));
	}
}
